import 'dart:ffi';
import 'dart:io';

import 'package:cbec_app/components/app_conifg.dart';
import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/step.dart';
import 'package:cbec_app/components/order/map_transport_box.dart';
import 'package:cbec_app/components/order/order_goods_item.dart';
import 'package:cbec_app/pages/goods/controller/after_sale_ctr.dart';
import 'package:cbec_app/pages/my/view/courier_selection_widget.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/AppData.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

//售后详情
class AfterSalesDetail extends GetView<AfterSaleController> {
  const AfterSalesDetail({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AfterSaleController>(builder: (controller) {
      if (controller.data != null) {
        return BasePage(
          title: "售后详情",
          iconType: IconType.back,
          children: [
            Container(
              decoration: border_radis(Colors.white),
              margin: EdgeInsets.fromLTRB(8.w, 10.w, 8.w, 10.w),
              padding: EdgeInsets.all(10.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("退款金额", style: setTextStyle(14, FontWeight.w300, Tools.fromHex("#333333"))),
                  SizedBox(height: 12.w),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [Text("${getPaytype(controller.payType)}支付(1-3个工作日到账)", style: setTextStyle(12, FontWeight.w300, Tools.fromHex("#666666"))), Text("￥${controller.data?['amount']}", style: setTextStyle(14, FontWeight.w300, Tools.fromHex("#F83F2D")))],
                  ),
                  SizedBox(height: 11.w),
                  Container(
                    width: double.infinity,
                    decoration: border_radis(Tools.fromHex("#F8F8F8")),
                    padding: EdgeInsets.fromLTRB(18.w, 10.w, 0.w, 10.w),
                    child: Text("退款至${getPaytype(controller.payType)}", style: setTextStyle(12, FontWeight.w300, Tools.fromHex("#666666"))),
                  )
                ],
              ),
            ),

            //步骤
            Container(
              decoration: border_radis(Tools.fromHex("#FF7254")),
              margin: EdgeInsets.fromLTRB(8.w, 0.w, 8.w, 10.w),
              padding: EdgeInsets.all(10.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("服务进度", style: setTextStyle(14, FontWeight.w500, Colors.white)),
                  StepsWidget(currentIndex: controller.refundType - 1, items: ["提交申请", "客服审核", "商家收货", "商家处理", "完成"]) //接口该返回这些步骤，但是没返回
                ],
              ),
            ),

            //商品
            Container(
              margin: EdgeInsets.only(left: 8.w, right: 8.w),
              decoration: border_radis(Colors.white),
              padding: EdgeInsets.all(10.w),
              child: Column(
                children: [
                  for (var goodsItem in controller.data?['goodsInfo']) OrderGoodsItem(goodsImg: goodsItem['img'], title: goodsItem['goodsName'], tags: [getGoodsTypeName(goodsItem['commodityType'])], price: goodsItem['price'], num: goodsItem['specNum']),
                  BaseInteractive(
                    onClick: () => {Get.toNamed(Routes.serviceSupportPage)},
                    childWidget: Container(
                      padding: EdgeInsets.only(top: 10.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          BaseImage(url: "customer.png", width: 14, height: 14),
                          SizedBox(
                            width: 10.w,
                          ),
                          Text("联系客服", style: setTextStyle(13, FontWeight.w500, Tools.fromHex("#333333")))
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),

            //订单信息
            Container(
              decoration: border_radis(Colors.white),
              margin: EdgeInsets.fromLTRB(8.w, 10.w, 8.w, 10.w),
              padding: EdgeInsets.all(10.w),
              child: Column(
                children: [
                  _listItem("退款金额", "￥${controller.data?['amount']}"),
                  _listItem("订单号", controller.data?['sn']),
                  _listItem("下单时间", controller.data?['cd']),
                  _listItem("申请时间", controller.data?['refundCd']),
                  _listItem(
                      "服务类型",
                      controller.refundType == 1
                          ? "退货退款"
                          : controller.refundType == 2
                              ? "云仓直接退款"
                              : "换货"),
                  _listItem("退款原因", getReason(controller.data?['reasonWay'])),
                  _listItem("退款方式", "原支付返回"),
                  _listItem("预计退款至", getPaytype(controller.payType))
                ],
              ),
            ),

            //退款物流
            Container(
              decoration: border_radis(Colors.white),
              margin: EdgeInsets.only(left: 8.w, right: 8.w),
              padding: EdgeInsets.all(10.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("上传退货单号", style: setTextStyle(14, FontWeight.w500, Tools.fromHex("#3D3D3D"))),
                  SizedBox(
                    height: 10.w,
                  ),
                  BaseInteractive(
                    onClick: () {
                      // Tools.showToast("没有接口，bro，选不了哦");
                    },
                    childWidget: const Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Text("物流公司"),
                            Text("*"),
                          ],
                        ),
                        CourierSelectionWidget(),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 10.w,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        width: 240.w,
                        padding: EdgeInsets.only(top: 10.w, bottom: 10.w, left: 10.w),
                        decoration: border_radis(Tools.fromHex("#F8F8F8")),
                        child: TextField(
                          controller: controller.logisticsTec,
                          decoration: descript("请输入快递单号码"),
                        ),
                      ),
                      solidButton("确认", width: 80.w, height: 40, () {
                        if (controller.logisticsTec.text.toString().isNotEmpty) {
                          controller.shipping(
                            {
                              'expressName': controller.logisticsSelectedDataa,
                              'expressNumber': controller.logisticsTec.text,
                              'refundId': controller.data?['id'],
                            },
                          );
                        } else {
                          Tools.showToast('请输入快递单号');
                        }
                      })
                    ],
                  ),
                  SizedBox(
                    height: 40.h,
                  ),
                  //下面是物流组件
                  // MapTransportBox(showMap: false,expresName: "顺丰快递",expressCode: "32454534231", transportList: [])
                ],
              ),
            ),

            // Container(
            //   alignment: Alignment.centerRight,
            //   padding: EdgeInsets.fromLTRB(0.w, 8.w, 10.w, 8.w),
            //   color: Tools.fromHex("#F3F5F7"),
            //   child: solidButton(
            //       "取消申请",
            //       width: 72.w,
            //       height: 35.h,
            //       borderColor: Tools.fromHex("#F83F2D"),
            //       textColor: Tools.fromHex("#F83F2D"),
            //       backgroundColor: Tools.fromHex("#F3F5F7"),
            //       () {}),
            // )
          ],
        );
      } else {
        return Text('');
      }
    });
  }

  ///获取退款方式
  String getPaytype(int payType) {
    String type = '微信零钱';
    switch (payType) {
      case 1:
        type = "微信零钱";
        break;
      case 2:
        type = "支付宝";
        break;
      case 3:
        type = "银联";
        break;
      case 4:
        type = "余额";
        break;
      default:
        type = "原路";
    }
    return type;
  }

  ///获取退款原因
  String getReason(int reasonWay) {
    String type = '无';
    switch (reasonWay) {
      case 1:
        type = "七天无理由";
        break;
      case 2:
        type = "商品与描述不符";
        break;
      case 3:
        type = "价格变化";
        break;
      case 4:
        type = "少发/错发/未收到";
        break;
      case 5:
        type = "商品质量/故障";
        break;
      case 6:
        type = "商品破损/包装问题";
        break;
      default:
        type = "无";
    }
    return type;
  }

  //列表项
  Widget _listItem(String left, String right) {
    return Container(
      margin: EdgeInsets.only(bottom: 10.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [Text(left, style: setTextStyle(14, FontWeight.w300, Tools.fromHex("#666666"))), Text(right, style: setTextStyle(13, FontWeight.w500, Tools.fromHex("#3D3D3D")))],
      ),
    );
  }
}
